package weekMatch.two_105;

import java.util.Arrays;
import java.util.List;

public class LeeCode_03 {
    public static void main(String[] args) {
//        System.out.println(maxStrength(new int[]{-4, -5, -4}));
//        System.out.println(maxStrength(new int[]{-4, -3}));
//        System.out.println(maxStrength(new int[]{0, -1}));
        System.out.println(maxStrength(new int[]{-9}));
    }
    private static long maxStrength(int[] nums) {
        if (nums.length == 1)
            return nums[0];
        Arrays.sort(nums);
        int n = nums.length, mid = -1;
        for (int i = 0; i < n; i++) {
            if (nums[i] >= 0){
                mid = i;
                break;
            }
        }
        mid = mid == -1 ? n : mid;
        long ans = 1;
        if (mid >= 2){
            if (mid % 2 == 0){
                for (int i = 0; i < mid; i++) {
                    ans *= nums[i];
                }
            }else {
                for (int i = 0; i < mid - 1; i++) {
                    ans *= nums[i];
                }
            }
        }
        for (int i = mid; i < n; i++) {
            if (nums[i] > 0){
                ans *= nums[i];
            }
        }
        if (mid < 2 && nums[n - 1] == 0){
            return 0;
        }
        return ans;
    }
}
